package com.saver.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.saver.R;
import com.saver.core.util.CommonUtil;
import com.saver.view.TransactionListActivity;
import com.saver.view.gui.CircleView;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by HGonzales.
 */
public class TransactionListAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;

    private ArrayList<Map<String, String>> data;

    public TransactionListAdapter(Activity activity, ArrayList<Map<String, String>> data) {
        this.data = data;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_detail, null);
        }

        TextView nombreCategoria = (TextView) convertView.findViewById(R.id.nombre_categoria);
        TextView nombreEstablecimiento = (TextView) convertView.findViewById(R.id.nombre_establecimiento);
        TextView monto = (TextView) convertView.findViewById(R.id.monto);
        TextView fecha = (TextView) convertView.findViewById(R.id.fecha);
        CircleView icono = (CircleView) convertView.findViewById(R.id.icon_category);

        Map<String, String> currentData = data.get(position);
        nombreCategoria.setText(CommonUtil.trim(currentData.get(TransactionListActivity.KEY_CATEGORY_NAME)).toUpperCase());
        nombreEstablecimiento.setText(CommonUtil.trim(currentData.get(TransactionListActivity.KEY_INSTITUTION)).toUpperCase());
        monto.setText(CommonUtil.trim(currentData.get(TransactionListActivity.KEY_MONEY_IMPORT)));
        fecha.setText(CommonUtil.trim(currentData.get(TransactionListActivity.KEY_EVENT_DATE)).toLowerCase());
        String[] rgb = currentData.get(TransactionListActivity.KEY_COLOR).split(",");
        int color = Color.rgb(Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2]));
        icono.setTitleText(CommonUtil.trim(currentData.get(TransactionListActivity.KEY_CATEGORY_NAME)).substring(0, 1).toUpperCase());
        icono.setStrokeColor(color);
        icono.setTitleColor(color);
        icono.setFillColor(color & 0x60FFFFFF);

        convertView.setTag(currentData.get(TransactionListActivity.KEY_TRANSACTION));
        return convertView;
    }
}
